
package classes;

import javax.swing.*;
import java.awt.event.*;
import components.CImage;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;

public class MainFrame extends JFrame {

    JLayeredPane layeredPane = new JLayeredPane();
    CImage bg;
    CImage darkBox;
    CImage lightBox;
    CImage lightBox2;
    CImage darkBox2;
    MenuPanel menu = new MenuPanel();
    AddPatientPanel addPatient = new AddPatientPanel();  
    ViewPatientRecordPanel viewRecord = new ViewPatientRecordPanel();
    AddServicePanel addService = new AddServicePanel();
    PatientManagementPanel managePatient = new PatientManagementPanel();
    ReportsPanel report = new ReportsPanel();
    EditServicePanel editService = new EditServicePanel();
    

    public MainFrame()
    {

        /**************************
        *       LAYOUT            *
        **************************/
        
        //Frame Properties
        setSize(1000, 700);
        setResizable(false);
        setTitle("Community Dental Clinic");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);

        add(layeredPane);

        //FrameIcon
        setIcon();

        //BackGround CImage
        setBg("bg.png");
        
        layeredPane.add(menu);
        layeredPane.setLayer(menu, 1);
        menu.setBounds(0, 0, getWidth(), getHeight());

        darkBox = new CImage("darkbox.png" ,1000,700);
        layeredPane.add(darkBox);
        layeredPane.setLayer(darkBox,2);
        darkBox.setLocation(0,0);
        darkBox.setVisible(false);

        lightBox = new CImage("lightbox.png" ,740, 655);
        layeredPane.add(lightBox);
        layeredPane.setLayer(lightBox,3);
        lightBox.setLocation(250, 10);
        lightBox.setVisible(false);
        
        
        layeredPane.add(viewRecord);
        layeredPane.setLayer(viewRecord, 4);
        viewRecord.setLocation(260, 10);
        
        layeredPane.add(managePatient);
        layeredPane.setLayer(managePatient, 4);
        managePatient.setLocation(260, 10);
        
        layeredPane.add(report);
        layeredPane.setLayer(report, 4);
        report.setLocation(260, 10);
        
        darkBox2 = new CImage("darkbox.png" ,1000,700);
        layeredPane.add(darkBox2);
        layeredPane.setLayer(darkBox2,5);
        darkBox2.setLocation(0,0);
        darkBox2.setVisible(false);

        lightBox2 = new CImage("lightbox2.png" ,500,500);
        layeredPane.add(lightBox2);
        layeredPane.setLayer(lightBox2,6);
        lightBox2.setLocation(370, 90);
        lightBox2.setVisible(false);
        
        layeredPane.add(addPatient);
        layeredPane.setLayer(addPatient, 7);
        addPatient.setLocation(370, 90);
        
         layeredPane.add(addService);
        layeredPane.setLayer(addService, 7);
        addService.setLocation(370, 90);
        
        layeredPane.add(editService);
        layeredPane.setLayer(editService, 7);
        editService.setLocation(370, 90);

        
        /**************************
        *          EVENTS         *
        *   For Page Navigations  *
        **************************/      
        /*** events for menu panel***/

        managePatient.btnAddPatient.addActionListener(new  ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
              if(managePatient.isVisible() == true)
              {
               darkBox2.setVisible(true);
               lightBox2.setVisible(true);
               addPatient.setVisible(true);
              }
             
            }
        }
        );
        
        managePatient.btnViewPatient.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(managePatient.isVisible() == true && addPatient.isVisible() == false) {
                    if(managePatient.isTableSelected() == true) {
                        managePatient.setVisible(false);
                        viewRecord.initValues();
                        viewRecord.setVisible(true);
                        viewRecord.setRecordsTable();
                        viewRecord.disableEdit();
                    }
                    else{
                        JOptionPane.showMessageDialog(managePatient, "Please select a patient to view", "Error Message", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        });
        
       menu.btnPatient.addActionListener(new  ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
              if(darkBox.isVisible() == false &&  lightBox.isVisible() == false)
              {
               darkBox.setVisible(true);
               lightBox.setVisible(true);
               managePatient.setVisible(true);
              }
             
            }
        }
        );
       
       menu.btnReport.addActionListener(new  ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
              if(darkBox.isVisible() == false &&  lightBox.isVisible() == false)
              {
               darkBox.setVisible(true);
               lightBox.setVisible(true);
               report.setVisible(true);
              }
             
            }
        }
        );
        
          menu.btnExit.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
              if(darkBox.isVisible() == false &&  lightBox.isVisible() == false)
              {
                int response = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit?", "Confirm",
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
                if (response == JOptionPane.YES_OPTION) {
                    System.exit(0);
                }
              }
             
            }
        }
        );
          

        /***events for add patient panel***/

        addPatient.btnCancel.addActionListener(new  ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
              if(addPatient.isVisible() == true)
              {
               addPatient.resetComp();
               addPatient.resetBG();
               darkBox2.setVisible(false);
               lightBox2.setVisible(false);
               addPatient.setVisible(false);
               managePatient.repaint();
               managePatient.setVisible(true);
              }

            }
        }
        );
        
         /***events for view patient panel***/
         
         viewRecord.btnClosePanel.addActionListener(new  ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(viewRecord.isVisible() == true && addService.isVisible()==false && editService.isVisible()==false) {
                    viewRecord.setVisible(false);
                    managePatient.setPatientTable();
                    managePatient.repaint();
                    managePatient.setVisible(true);
                   
                }

            }
        });
         
          viewRecord.btnAddService.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                 if(viewRecord.isVisible() == true && editService.isVisible()==false) {
                      darkBox2.setVisible(true);
                      lightBox2.setVisible(true);
                     addService.setVisible(true);
                 }
             }
         });
          
          viewRecord.btnEditService.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                 if(viewRecord.isVisible() == true && addService.isVisible()==false && editService.isVisible() ==false) {
                      
                     if(viewRecord.isTableSelected() == true)
                     {
                      darkBox2.setVisible(true);
                      lightBox2.setVisible(true);
                      editService.setVisible(true);
                      Main.frame.editService.initValue();
                     }
                     else{
                    JOptionPane.showMessageDialog(viewRecord, "Please select a record to edit", "Error Message", JOptionPane.ERROR_MESSAGE);
                    }
                 }
               
             }
         });
         
         /***events for add service panel***/   
         addService.btnCancel.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                 if(addService.isVisible() == true) {
                     addService.resetComp();
                     addService.resetBG();
                     darkBox2.setVisible(false);
                     lightBox2.setVisible(false);
                     addService.setVisible(false);
                     
                 }
             }
         });
         
          editService.btnCancel.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                 if(editService.isVisible() == true) {
                     editService.resetComp();
                     editService.resetBG();
                     darkBox2.setVisible(false);
                     lightBox2.setVisible(false);
                     editService.setVisible(false);
                     viewRecord.repaint();
                     viewRecord.setVisible(true);
                 }
             }
         });
         
        
        /***events for manage patient panel***/   
        
          managePatient.btnCancel.addActionListener(new  ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
              if(managePatient.isVisible() == true && addPatient.isVisible()== false)
              {
               darkBox.setVisible(false);
               lightBox.setVisible(false);
               managePatient.selectFilter.setSelectedIndex(0);
               managePatient.setVisible(false);

              }

            }
        }
        );
        
         /***events for sales panel***/
          
        report.btnCancel.addActionListener(new  ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
              if(report.isVisible() == true)
              {
               darkBox.setVisible(false);
               lightBox.setVisible(false);
               report.setVisible(false);
               report.resetComponents();
              }

            }
        }
        );
        
    }


     /**************************
     *          METHODS        *
     **************************/
    
        public final void setIcon() {
        BufferedImage image = null;
        try {
            image = ImageIO.read(getClass().getResource("/images/logo.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        setIconImage(image);
        }

        public final void setBg(String path){
        bg = new CImage(path ,1000,700);
        layeredPane.add(bg);
        layeredPane.setLayer(bg,-1,-1);
        bg.setLocation(0,0);
        }
        
}
